package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
同步工作日历
*/
public class SyncCalendar implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(SyncCalendar.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			PropertiesUtil properties = new PropertiesUtil();
			def ibqurl = properties.readValue("ibqurl"); //获取webservice接口地址
			def appid = properties.readValue("appid"); //获取appId
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date now = new Date();
			Calendar c = Calendar.getInstance();
			c.setTime(now);
			c.set(Calendar.DAY_OF_YEAR, 1);		
			def startdate = format.format(c.getTime()); //获取开始日期
			c.add(Calendar.YEAR, 2);
			c.set(Calendar.DAY_OF_YEAR, -1);
			def enddate = format.format(c.getTime()); //获取结束日期
			def querysql = "";
			def sqlresult = "";
			querysql = "delete from c_calendar where date_d>=to_date('"+startdate+"','yyyy-mm-dd') and date_d<=to_date('"+enddate+"','yyyy-mm-dd')";
			sqlresult = eliteBean.update(querysql,"dataSource");
			querysql = "delete from c_freedate where freedate>=to_date('"+startdate+"','yyyy-mm-dd') and freedate<=to_date('"+enddate+"','yyyy-mm-dd')";
			sqlresult = eliteBean.update(querysql,"dataSource");
			startdate = startdate.replaceAll('-','');
			enddate =enddate.replaceAll('-','');
			def xmlBody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<SOAP-ENV:Body>
		<m:query xmlns:m="http://service.web.server.ua.businessmatrix.com/">
			<appId>"""+appid+"""</appId>
			<name>s-WORKDATE</name>
			<params>
				<name>START_DATE</name>
				<operator>notLess</operator>
				<value>"""+startdate+"""</value>
			</params>
			<params>
				<name>END_DATE</name>
				<operator>notMore</operator>
				<value>"""+enddate+"""</value>
			</params>			
		</m:query>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>""";									
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			xmlBody = Util.callWebService(ibqurl,xmlBody,charset,"",timeout);
			xmlBody = Util.Transform(xmlBody);
			xmlBody = new XmlParser().parseText(xmlBody);
			def workflag = "";
			xmlBody.Body.queryResponse.QueryResult.rows.each{
			workflag = it.item[0].text();
			querysql = "insert into c_calendar(date_i,date_d,workflag) values ("+it.item[1].text()+",to_date('"+it.item[1].text()+"','yyyy-mm-dd'),"+workflag+")";
			sqlresult = eliteBean.update(querysql,"dataSource");
			if(workflag.equals('0')){
				querysql = "insert into c_freedate(freedate) values (to_date('"+it.item[1].text()+"','yyyy-mm-dd'))";
				sqlresult = eliteBean.update(querysql,"dataSource");	
			}
			};
			return "{\"msg\": \"请求接口成功！\",\"code\": 1}";
		}catch(Exception e) {
			
		}
	}	
}
